<html>
<head><meta charset="utf-8"><title>What are (min) const generics good for? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html">What are (min) const generics good for?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234164226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164226">(Apr 12 2021 at 14:05)</a>:</h4>
<p>I've got another round of training this week, and notably this is the first session since const generics are available in a stable compiler. In my own work, I've usually gone one way: I have a problem and realize it could be solved through some use of const generics.</p>
<p>Now I'm looking for the opposite mapping — what are some <em>classes</em> of problems that are solved given const generics? Ideally those only requiring min const generics. Bonus points for also touching on why other languages (especially Java) don't need / care about such a feature.</p>



<a name="234164441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164441">(Apr 12 2021 at 14:06)</a>:</h4>
<p>abstracting over array lengths is usually the root problem they solve</p>



<a name="234164494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164494">(Apr 12 2021 at 14:07)</a>:</h4>
<p>@_<strong>Jonas Schievink  [he&#x2F;him]|211727</strong> <a href="#narrow/stream/122651-general/topic/What.20are.20.28min.29.20const.20generics.20good.20for.3F/near/234164441">said</a>:</p>
<blockquote>
<p>abstracting over array lengths is usually the root problem they solve</p>
</blockquote>
<p>e.g. all the trait impls in libstd that are now stable</p>



<a name="234164505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164505">(Apr 12 2021 at 14:07)</a>:</h4>
<p>The biggest one I can think of is the ability to generalize over arrays of any length, but this feels <em>a little</em> circular because Rust chose the path of baking the array length into the type to start with. We caused ourselves a problem and then eventually fixed it <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span>. It's still useful in the long run though.</p>



<a name="234164841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164841">(Apr 12 2021 at 14:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/What.20are.20.28min.29.20const.20generics.20good.20for.3F/near/234164505">said</a>:</p>
<blockquote>
<p>The biggest one I can think of is the ability to generalize over arrays of any length, but this feels <em>a little</em> circular because Rust chose the path of baking the array length into the type to start with. We caused ourselves a problem and then eventually fixed it <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span>. It's still useful in the long run though.</p>
</blockquote>
<p>right - in java, all arrays are essentially <code>[T]</code> in rust, it actually uses arrays to implement varargs</p>



<a name="234164867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164867">(Apr 12 2021 at 14:08)</a>:</h4>
<p>generating distinct optimized versions of a function depending on an integer is another thing they let you do</p>



<a name="234164966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234164966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234164966">(Apr 12 2021 at 14:09)</a>:</h4>
<p>@_<strong>Jonas Schievink  [he&#x2F;him]|211727</strong> <a href="#narrow/stream/122651-general/topic/What.20are.20.28min.29.20const.20generics.20good.20for.3F/near/234164867">said</a>:</p>
<blockquote>
<p>generating distinct optimized versions of a function depending on an integer is another thing they let you do</p>
</blockquote>
<p>(although people are complaining about that too :P) <a href="https://github.com/rust-lang/rust/issues/77767">https://github.com/rust-lang/rust/issues/77767</a></p>



<a name="234165086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234165086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234165086">(Apr 12 2021 at 14:10)</a>:</h4>
<p>of course, no good deed goes unpunished</p>



<a name="234165420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234165420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234165420">(Apr 12 2021 at 14:12)</a>:</h4>
<p>@_<strong>Jonas Schievink  [he&#x2F;him]|211727</strong> <a href="#narrow/stream/122651-general/topic/What.20are.20.28min.29.20const.20generics.20good.20for.3F/near/234164867">said</a>:</p>
<blockquote>
<p>generating distinct optimized versions of a function depending on an integer is another thing they let you do</p>
</blockquote>
<p>Do you have any specific instances of this that I could point at?</p>
<p>One that I had thought of was parameterizing the <code>B</code> in <code>BTreeMap</code>, but I don't think that's been done (or people aren't interested in it for some reason)</p>



<a name="234165607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234165607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234165607">(Apr 12 2021 at 14:13)</a>:</h4>
<p>probably some stuff in here? <a href="https://github.com/rust-ndarray/ndarray/issues/961">https://github.com/rust-ndarray/ndarray/issues/961</a></p>



<a name="234165786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234165786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234165786">(Apr 12 2021 at 14:14)</a>:</h4>
<p>I'll skim that, thank you. On first blush, they all look like more usages of arrays in different forms.</p>



<a name="234165905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234165905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234165905">(Apr 12 2021 at 14:14)</a>:</h4>
<p>yeah, maybe that's not it</p>



<a name="234166315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234166315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234166315">(Apr 12 2021 at 14:16)</a>:</h4>
<p>There was a SO question a while ago that boiled down to using enum variants as const generic values (which you can't do with min-const-generics), but I can't find it now.</p>



<a name="234166555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234166555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234166555">(Apr 12 2021 at 14:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/What.20are.20.28min.29.20const.20generics.20good.20for.3F/near/234166315">said</a>:</p>
<blockquote>
<p>There was a SO question a while ago that boiled down to using enum variants as const generic values (which you can't do with min-const-generics), but I can't find it now.</p>
</blockquote>
<p>sounds like typestate sort of</p>



<a name="234166580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234166580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234166580">(Apr 12 2021 at 14:17)</a>:</h4>
<p>but with enum variants instead of ZSTs</p>



<a name="234166759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234166759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234166759">(Apr 12 2021 at 14:18)</a>:</h4>
<p>would they have a size, since they are just generics... <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="234166796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234166796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234166796">(Apr 12 2021 at 14:19)</a>:</h4>
<p>I guess it depends on if you ever instantiate one</p>



<a name="234166882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234166882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234166882">(Apr 12 2021 at 14:19)</a>:</h4>
<p>e.g. <code>struct Foo&lt;const N: usize&gt;;</code> doesn't take up <code>usize</code>'s bytes.</p>



<a name="234167675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234167675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234167675">(Apr 12 2021 at 14:24)</a>:</h4>
<p>they don't have a size, they're only part of the type, not the value</p>



<a name="234168505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234168505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234168505">(Apr 12 2021 at 14:28)</a>:</h4>
<p>Right. So it's not <em>quite</em> valid to say this, but it almost makes any value / type "zero-sized" because it's lifted out of the value space and into the type space.</p>



<a name="234169258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234169258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234169258">(Apr 12 2021 at 14:32)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Here are a couple of real life examples that I know of which already use min const generics:</p>
<p><a href="https://docs.rs/elain/0.2.0/elain/struct.Align.html">https://docs.rs/elain/0.2.0/elain/struct.Align.html</a> Set a type's minimum alignment with const generics (instead of through <code>#[repr(align(N))]</code>). Also statically asserts that the alignment is a power of 2 (indeed the <code>Align</code> type is zero-sized, but it has an alignment greater than 1).</p>
<p><a href="https://docs.rs/deferred-reference/0.1.2/deferred_reference/trait.PointerLength.html">https://docs.rs/deferred-reference/0.1.2/deferred_reference/trait.PointerLength.html</a> An abstraction to get the length of both arrays and slices without creating an intermediate reference to its contents. This is then re-used in the same crate in order to slice into the slice/array, but with returning a pointer instead of a reference, also without creating intermediate references, in <a href="https://docs.rs/deferred-reference/0.1.2/deferred_reference/trait.SlicePointerIndex.html">https://docs.rs/deferred-reference/0.1.2/deferred_reference/trait.SlicePointerIndex.html</a> (apologies for the shameless plug <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span>  ).</p>



<a name="234169747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234169747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234169747">(Apr 12 2021 at 14:35)</a>:</h4>
<p>We have used it recently to replace manual specialization of memcmp for fixed length: <a href="https://github.com/cloudflare/sliceslice-rs/commit/5b8181515523630f4ae8a015e0fd0666d042ab6f">https://github.com/cloudflare/sliceslice-rs/commit/5b8181515523630f4ae8a015e0fd0666d042ab6f</a></p>



<a name="234171271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234171271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234171271">(Apr 12 2021 at 14:44)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> hmm. Couldn't you have just been calling <code>slice::from_raw_parts(left, {0, 1, 2, ...})</code> in your macro anyway? I assume you have benchmarks for that piece of code — were there any interesting results?</p>



<a name="234171413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234171413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234171413">(Apr 12 2021 at 14:45)</a>:</h4>
<p>Ah, here — <a href="https://github.com/cloudflare/sliceslice-rs/pull/36#issuecomment-817299026">https://github.com/cloudflare/sliceslice-rs/pull/36#issuecomment-817299026</a></p>



<a name="234171492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234171492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234171492">(Apr 12 2021 at 14:45)</a>:</h4>
<p><code>+0.00914766039056758%</code>; seems in the noise threshold</p>



<a name="234171660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234171660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234171660">(Apr 12 2021 at 14:46)</a>:</h4>
<p>Yeah so the history of the crate has to be taken into account. We have been implementing in rust, a code originally implemented with C/C++ and we are slowly updating it to "rust standard"</p>



<a name="234171881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234171881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234171881">(Apr 12 2021 at 14:47)</a>:</h4>
<p>So yeah , we could have used a macro for the same thing. But precisely what const generics offer some time, is the ability to <em>avoid</em> macros</p>



<a name="234191007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234191007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234191007">(Apr 12 2021 at 16:40)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/What.20are.20.28min.29.20const.20generics.20good.20for.3F/near/234164226">said</a>:</p>
<blockquote>
<p>Bonus points for also touching on why other languages (especially Java) don't need / care about such a feature.</p>
</blockquote>
<p>Historically Java hasn't cared about offering non-boxed types, so if one is always fine with allocating, then a GC'd <code>int[]</code> of runtime size is fine.</p>
<p>The other common thing that gets used instead of const generics is just tuples.  Like you end up with <code>.pairs()</code> and <code>.triples()</code> methods giving <code>(T, T)</code> or <code>(T, T, T)</code> instead of offering something allowing <code>[T; N]</code> for multiple possible widths.</p>



<a name="234191364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234191364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234191364">(Apr 12 2021 at 16:42)</a>:</h4>
<p>Honestly I think that because of the <code>min_</code>, most of the uses are going to boil down to basically arrays.</p>
<p>The versions of const generic that get far more exciting tend to need induction, but that's not available yet.</p>



<a name="234256049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234256049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234256049">(Apr 13 2021 at 01:43)</a>:</h4>
<p>It has been _very_ useful for writing the SIMD API.<br>
SIMD is "basically just arrays", fwiw, but you can't really do the same _stuff_ with "<code>[T]</code>" instead of "<code>[T; N]</code>" re: SIMD, because you need the <code>N</code> anyways to be able to vectorize to fixed-width things, so you fall back to something like <code>[(T, T, T, T)]</code>. This makes your arrays unnecessarily "multidimensional" as a result.</p>



<a name="234259866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234259866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234259866">(Apr 13 2021 at 02:41)</a>:</h4>
<p>It's going to prove very useful with anything where you have those kinds of real numeric constraints in terms of memory manipulation, I expect, especially as it grows in use? As far as SIMD performance tho', sadly, we have other issues penalizing such benches as well.</p>



<a name="234260547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234260547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234260547">(Apr 13 2021 at 02:52)</a>:</h4>
<p>You can use it to obtain multiple mutable references from a <code>HashMap</code>: <a href="https://docs.rs/hashbrown/0.11.2/hashbrown/hash_map/struct.HashMap.html#method.get_each_mut">https://docs.rs/hashbrown/0.11.2/hashbrown/hash_map/struct.HashMap.html#method.get_each_mut</a></p>



<a name="234309063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20are%20%28min%29%20const%20generics%20good%20for%3F/near/234309063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20are.20(min).20const.20generics.20good.20for.3F.html#234309063">(Apr 13 2021 at 11:36)</a>:</h4>
<blockquote>
<p>The biggest one I can think of is the ability to generalize over arrays of any length, but this feels a little circular because Rust chose the path of baking the array length into the type to start with. We caused ourselves a problem and then eventually fixed it </p>
</blockquote>
<p>To be honest, this is definitely the major motivation for <code>min_const_generics</code>, and it's nothing to scoff at: arrays now have fully become first-class citizens, and this allows a lot of nice array-based shenanigans that we couldn't really do and that have been leading to an excessive use of things such as <code>Vec</code>tors. Yes, currently <code>min_const_generics</code> is palliating the initial limited nature of arrays in the type system (compared to other languages), but remember that these initial limitations originate from featuring all the following three things, which, AFAIK, no other language has done:</p>
<ul>
<li>the super explicit semantics of Rust types (no implicit boxing, <em>etc.</em>);</li>
<li>compile-time (<em>a.k.a.,</em> "zero-cost") polymorphism / meta-programming tool;</li>
<li>the fact that the main approach for this meta-programming tool, type generics, is <em>eagerly-checked</em> (type generics ≠ macros or C++ templates).</li>
</ul>
<p>You will notice that if we forego that third requirement, then Rust was already able to use arrays in a "polymorphic fashion" thanks to macros (<a href="https://docs.rs/uninit/0.4.0/uninit/macro.uninit_array.html">example</a>).</p>
<p>So baking the array length within an eagerly-checked generic type system has been a very pioneer thing to do, which definitely has its advantages, but has also had the drawback of not playing well with generics at the time. Now, with <code>min_const_generics</code>, that has been "fixed".</p>
<p>In other words, there is no shame in admitting that at least 95% of what <code>min_const_generics</code> enable is genericity over the fixed-size of an array <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> </p>
<hr>
<p>The next steps will definitely be a polished design of <code>const_evaluatable_checked</code> to allow for inductive things over arrays, and also supporting const <code>&amp;str</code> generics, since that will finally enable nicely expressing within the type system that certain types have certain fields, which allow very nice anonymous struct design, <em>etc.</em> (note that most of this stuff can already be done using <em>ad-hoc</em> types and helper traits with associated <code>const</code>ants, but it was cumbersome, and the error messages were not necessarily very readable).</p>
<p>Being generic over custom enums will also be nice, although that one is easy to <a href="https://github.com/napi-rs/napi-rs/blob/main/napi/src/threadsafe_function.rs#L39-L99">polyfill using classic generics and a sealed trait</a>, for the most part (the only thing lacking being the trait solver observing that an impl over all variants is an exhaustive impl over that <em>kind</em>, which the current polyfill cannot do, but AFAIK, the current implementation of <code>const_generics</code> doesn't achieve that yet either).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>